package cn.jekun.shop.web.action;

import javax.annotation.Resource;

import net.sf.json.JSONObject;
import cn.jekun.component.base.bean.ErrorMsg;
import cn.jekun.component.base.exceptions.BusinessException;
import cn.jekun.shop.common.service.GoodsService;
import cn.jekun.shop.common.service.ShopBaseAction;

/**
 * 商品展示
 * @author twj
 *
 */
@SuppressWarnings("serial")
public class GoodsAction extends ShopBaseAction{
	@Resource(name = GoodsService.BEAN_NAME)
	protected GoodsService goodsService;
	/**
	 * 商品列表
	 * @return
	 */
	public String goodsList(){
		JSONObject jsonObject = null;
		try{
			String pageSize = this.request.getParameter("pageSize");//页大小
			String currentPage = this.request.getParameter("currentPage");//当前页
			String goodType = this.request.getParameter("goodType");//商品类型(商家自定义)
			String regionId = this.request.getParameter("regionId");//地区编码
			String distance = this.request.getParameter("distance");//距离
			String lng = this.request.getParameter("lng");//经度
			String lat = this.request.getParameter("lat");//纬度
			String shopId = this.request.getParameter("shopId");//商户编码
			String cityId = this.request.getParameter("cityId");//已开通城市代码
			String shopType = this.request.getParameter("shopType");//商家分类类型
			String sort = this.request.getParameter("sort");//排序字段
			String goodName = this.request.getParameter("goodName");//商品名称
			String barCode = this.request.getParameter("barCode");//条形码
			String sessionId = this.request.getSession().getId();
			String userId = this.getUserId();//条形码
			
			
			jsonObject = goodsService.queryGoods(pageSize,currentPage,goodType,shopType,cityId,regionId,distance,lng,lat,shopId,sort,goodName,barCode,sessionId,userId);
		} catch (BusinessException e) {
			// TODO: handle exception
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}


	/**
	 * 商品详情
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String queryDetails() {
		JSONObject jsonObject = null;
		try {
			formMap.put("merchandiseId", request.getParameter("merchandiseId"));
			formMap.put("shopId", request.getParameter("shopId"));
			if(this.getSessionUser()!=null){
				formMap.put("username", "".equals(this.getSessionUser().getUserName())?"":this.getSessionUser().getUserName());
				formMap.put("userId","".equals(this.getUserId())?"":this.getUserId());
			}
			jsonObject = goodsService.querydetails(formMap);
		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}

}
